ggplot2ggplot2library(tidyverse)
library(knitr)
library(broom)
library(stringr)
options(digits = 3)
set.seed(1234)Google defines a grammar as “the whole system and structure of a language or of languages in general, usually taken as consisting of syntax and morphology (including inflections) and sometimes also phonology and semantics”.1 Others consider a grammar to be “the fundamental principles or rules of an art or science”.2 Applied to visualizations, a grammar of graphics is a grammar used to describe and create a wide range of statistical graphics.3
The layered grammar of graphics approach is implemented in ggplot2, a widely used graphics library for R. All graphics in this library are built using a layered approach, building layers up to create the final graphic.
Layers are used to create the objects on a plot. They are defined by five basic parts:
Layers are typically related to one another and share many common features. For instance, multiple layers can be built using the same underlying data. An example would be a scattterplot overlayed with a smoothed regression line to summarize the relationship between the variables:
Data defines the source of the information to be visualized, but is independent from the other elements. So a layered graphic can be built which can utilize different data sources while keeping the other components the same. Here is a portion of a dataset contained in the ggplot2 package: mpg.
head(mpg) %>%
kable(caption = "Dataset of automobiles")| manufacturer | model | displ | year | cyl | trans | drv | cty | hwy | fl | class |
|---|---|---|---|---|---|---|---|---|---|---|
| audi | a4 | 1.8 | 1999 | 4 | auto(l5) | f | 18 | 29 | p | compact |
| audi | a4 | 1.8 | 1999 | 4 | manual(m5) | f | 21 | 29 | p | compact |
| audi | a4 | 2.0 | 2008 | 4 | manual(m6) | f | 20 | 31 | p | compact |
| audi | a4 | 2.0 | 2008 | 4 | auto(av) | f | 21 | 30 | p | compact |
| audi | a4 | 2.8 | 1999 | 6 | auto(l5) | f | 16 | 26 | p | compact |
| audi | a4 | 2.8 | 1999 | 6 | manual(m5) | f | 18 | 26 | p | compact |
Mapping defines how the variables are applied to the graphic. So if we were graphing information from mpg, we might map a car’s engine displacement to the \(x\) position and highway mileage to the \(y\) position.
mpg %>%
select(displ, hwy) %>%
rename(x = displ,
y = hwy)## # A tibble: 234 × 2
## x y
## <dbl> <int>
## 1 1.8 29
## 2 1.8 29
## 3 2.0 31
## 4 2.0 30
## 5 2.8 26
## 6 2.8 26
## 7 3.1 27
## 8 1.8 26
## 9 1.8 25
## 10 2.0 28
## # ... with 224 more rows
A statistical transformation (stat) transforms the data, generally by summarizing the information. For instance, in a bar graph you typically are not trying to graph the raw data because this doesn’t make any inherent sense. Instead, you might summarize the data by graphing the total number of observations within a set of categories. Or if you have a dataset with many observations, you might transform the data into a smoothing line which summarizes the overall pattern of the relationship between variables.
A stat takes a dataset as input and returns a dataset as output, and so a stat can add new variables to the original dataset. So instead of graphing this data in its raw form:
mpg %>%
select(cyl)## # A tibble: 234 × 1
## cyl
## <int>
## 1 4
## 2 4
## 3 4
## 4 4
## 5 6
## 6 6
## 7 6
## 8 4
## 9 4
## 10 4
## # ... with 224 more rows
You would transform it to:
mpg %>%
count(cyl)## # A tibble: 4 × 2
## cyl n
## <int> <int>
## 1 4 81
## 2 5 4
## 3 6 79
## 4 8 70
Sometimes you don’t need to make a statistical transformation. For example, in a scatterplot you use the raw values for the \(x\) and \(y\) variables to map onto the graph. In these situations, the statistical transformation is an identity transformation - the stat simply passes in the original dataset and exports the exact same dataset.
Geometric objects (geoms) control the type of plot you create. Geoms are classified by their dimensionality:
A geom is the
ggplot2name for a “mark”.
Each geom can only display certain aesthetics. For example, a point geom has position, color, shape, and size aesthetics.
ggplot(mpg, aes(displ, hwy, color = class)) +
geom_point() +
ggtitle("A point geom with position and color aesthetics")A bar geom has position, height, width, and fill color.
ggplot(mpg, aes(cyl)) +
geom_bar() +
ggtitle("A bar geom with position and height aesthetics")Sometimes with dense data we need to adjust the position of elements on the plot, otherwise data points might obscure one another. Bar plots frequently stack or dodge the bars to avoid overlap:
count(mpg, class, cyl) %>%
ggplot(aes(cyl, n, fill = class)) +
geom_bar(stat = "identity") +
ggtitle("A stacked bar chart")count(mpg, class, cyl) %>%
ggplot(aes(cyl, n, fill = class)) +
geom_bar(stat = "identity", position = "dodge") +
ggtitle("A dodged bar chart")Sometimes scatterplots with few unique \(x\) and \(y\) values are jittered (random noise is added) to reduce overplotting.
ggplot(mpg, aes(cyl, hwy)) +
geom_point() +
ggtitle("A point geom with obscured data points")ggplot(mpg, aes(cyl, hwy)) +
geom_jitter() +
ggtitle("A point geom with jittered data points")A scale controls how data is mapped to aesthetic attributes, so we need one scale for every aesthetic property employed in a layer. For example, this graph defines a scale for color:
ggplot(mpg, aes(displ, hwy, color = class)) +
geom_point() +
guides(color = guide_legend(override.aes = list(size = 4)))Note that the scale is consistent - every point for a compact car is drawn in tan, whereas SUVs are drawn in pink. The scale can be changed to use a different color palette:
ggplot(mpg, aes(displ, hwy, color = class)) +
geom_point() +
scale_color_brewer(palette = "Dark2") +
guides(color = guide_legend(override.aes = list(size = 4)))Now we are using a different palette, but the scale is still consistent: all compact cars utilize the same color, whereas SUVs use a different color but each SUV uses the same color.
A geom is the
ggplot2name for a “channel”.
A coordinate system (coord) maps the position of objects onto the plane of the plot, and control how the axes and grid lines are drawn. Plots typically use two coordinates (\(x, y\)), but could use any number of coordinates. Most plots are drawn using the Cartesian coordinate system:
x1 <- c(1, 10)
y1 <- c(1, 5)
p <- qplot(x1, y1, geom = "blank", xlab = NULL, ylab = NULL) +
theme_bw()
p This system requires a fixed and equal spacing between values on the axes. That is, the graph draws the same distance between 1 and 2 as it does between 5 and 6. The graph could be drawn using a semi-log coordinate system which logarithmically compresses the distance on an axis:
p + coord_trans(y = "log10")Or could even be drawn using polar coordinates:
p + coord_polar()Faceting can be used to split the data up into subsets of the entire dataset. This is a powerful tool when investigating whether patterns are the same or different across conditions, and allows the subsets to be visualized on the same plot (known as conditioned or trellis plots). The faceting specification describes which variables should be used to split up the data, and how they should be arranged.
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_wrap(~class)Rather than explicitly declaring each component of a layered graphic (which will use more code and introduces opportunities for errors), we can establish intelligent defaults for specific geoms and scales. For instance, whenever we want to use a bar geom, we can default to using a stat that counts the number of observations in each group of our variable in the \(x\) position.
ggplot() +
layer(
data = mpg, mapping = aes(x = displ, y = hwy),
geom = "point", stat = "identity", position = "identity"
) +
scale_x_continuous() +
scale_y_continuous() +
coord_cartesian()The above code:
ggplot)layer)
mpg)mapping)geom = "point")stat = "identity" and position = "identity")scale_x_continuous and scale_y_continuous)coord_cartesian)How can we simplify this using intelligent defaults?
Using these defaults, we can rewrite the above code as:
ggplot() +
geom_point(data = mpg, mapping = aes(x = displ, y = hwy))Because multiple layers can use the same components (data, mapping, etc.), we can specify that information in the ggplot function rather than in the layer function:
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point()And as we will learn, function arguments in R use specific ordering, so we can omit the explicit call to data and mapping:
ggplot(mpg, aes(displ, hwy)) +
geom_point()With this specification, it is easy to build the graphic up with additional layers, without modifying the original code:
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_smooth()## `geom_smooth()` using method = 'loess'
Because we called aes(x = displ, y = hwy) within the ggplot function, it is automatically passed along to both geom_point() and geom_smooth(). If we fail to do this, we get an error:
ggplot(mpg) +
geom_point(aes(displ, hwy)) +
geom_smooth()## `geom_smooth()` using method = 'loess'
## Error: stat_smooth requires the following missing aesthetics: x, y
The graphic is notable for its representation in two dimensions of six types of data: the number of Napoleon’s troops; distance; temperature; the latitude and longitude; direction of travel; and location relative to specific dates.4
# get data on troop movements and city names
troops <- read_table("data/minard-troops.txt")## Parsed with column specification:
## cols(
## long = col_double(),
## lat = col_double(),
## survivors = col_integer(),
## direction = col_character(),
## group = col_integer()
## )
cities <- read_table("data/minard-cities.txt")## Parsed with column specification:
## cols(
## long = col_double(),
## lat = col_double(),
## city = col_character()
## )
troops## # A tibble: 51 × 5
## long lat survivors direction group
## <dbl> <dbl> <int> <chr> <int>
## 1 24.0 54.9 340000 A 1
## 2 24.5 55.0 340000 A 1
## 3 25.5 54.5 340000 A 1
## 4 26.0 54.7 320000 A 1
## 5 27.0 54.8 300000 A 1
## 6 28.0 54.9 280000 A 1
## 7 28.5 55.0 240000 A 1
## 8 29.0 55.1 210000 A 1
## 9 30.0 55.2 180000 A 1
## 10 30.3 55.3 175000 A 1
## # ... with 41 more rows
cities## # A tibble: 20 × 3
## long lat city
## <dbl> <dbl> <chr>
## 1 24.0 55.0 Kowno
## 2 25.3 54.7 Wilna
## 3 26.4 54.4 Smorgoni
## 4 26.8 54.3 Moiodexno
## 5 27.7 55.2 Gloubokoe
## 6 27.6 53.9 Minsk
## 7 28.5 54.3 Studienska
## 8 28.7 55.5 Polotzk
## 9 29.2 54.4 Bobr
## 10 30.2 55.3 Witebsk
## 11 30.4 54.5 Orscha
## 12 30.4 53.9 Mohilow
## 13 32.0 54.8 Smolensk
## 14 33.2 54.9 Dorogobouge
## 15 34.3 55.2 Wixma
## 16 34.4 55.5 Chjat
## 17 36.0 55.5 Mojaisk
## 18 37.6 55.8 Moscou
## 19 36.6 55.3 Tarantino
## 20 36.5 55.0 Malo-Jarosewii
Exercise: Write out what the grammar of graphics will look for this graph.
troopslat and long)survivorsdirectionidentitypathcitieslat and long)cityidentitytextpathFirst we want to build the layer for the troop movement:
plot_troops <- ggplot(troops, aes(long, lat)) +
geom_path(aes(size = survivors,
color = direction,
group = group))
plot_troopsNext let’s add the cities layer:
plot_both <- plot_troops +
geom_text(data = cities, aes(label = city), size = 4)
plot_bothNow that the basic information is on there, we want to clean up the graph and polish the visualization by:
plot_polished <- plot_both +
scale_size(range = c(0, 12),
breaks = c(10000, 20000, 30000),
labels = c("10,000", "20,000", "30,000")) +
scale_color_manual(values = c("tan", "grey50")) +
coord_map() +
labs(title = "Map of Napoleon's Russian campaign of 1812",
x = NULL,
y = NULL)
plot_polishedFinally we can change the default ggplot theme to remove the background and grid lines, as well as the legend:
plot_polished +
theme_void() +
theme(legend.position = "none")ggplot2Now let’s practice generating layered graphics in R using the data from Gapminder World, which compiles country-level data on quality-of-life measures.
gapminder datasetIf you have not already installed the gapminder package and you try to load it using the following code, you will get an error:
library(gapminder)Error in library(gapminder) : there is no package called ‘gapminder’
If this happens, install the gapminder package by running install.packages("gapminder") in your console.
Once you’ve done this, run the following code to load the gapminder dataset, the ggplot2 library, and a helper library for printing the contents of gapminder:
library(ggplot2)
library(tibble)
library(gapminder)
str(gapminder)## Classes 'tbl_df', 'tbl' and 'data.frame': 1704 obs. of 6 variables:
## $ country : Factor w/ 142 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ continent: Factor w/ 5 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
## $ year : int 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
## $ lifeExp : num 28.8 30.3 32 34 36.1 ...
## $ pop : int 8425333 9240934 10267083 11537966 13079460 14880372 12881816 13867957 16317921 22227415 ...
## $ gdpPercap: num 779 821 853 836 740 ...
gapminder## # A tibble: 1,704 × 6
## country continent year lifeExp pop gdpPercap
## <fctr> <fctr> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779
## 2 Afghanistan Asia 1957 30.3 9240934 821
## 3 Afghanistan Asia 1962 32.0 10267083 853
## 4 Afghanistan Asia 1967 34.0 11537966 836
## 5 Afghanistan Asia 1972 36.1 13079460 740
## 6 Afghanistan Asia 1977 38.4 14880372 786
## 7 Afghanistan Asia 1982 39.9 12881816 978
## 8 Afghanistan Asia 1987 40.8 13867957 852
## 9 Afghanistan Asia 1992 41.7 16317921 649
## 10 Afghanistan Asia 1997 41.8 22227415 635
## # ... with 1,694 more rows
Run
?gapminderin the console to open the help file for the data and definitions for each of the columns.
Using the grammar of graphics and your knowledge of the ggplot2 library, generate a series of graphs that explore the relationships between specific variables.
ggplot(data = gapminder, aes(x = continent, y = lifeExp)) +
geom_boxplot()ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
geom_point() +
geom_smooth()## `geom_smooth()` using method = 'gam'
color aesthetic:ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp, color = continent)) +
geom_point() +
geom_smooth()## `geom_smooth()` using method = 'loess'
With a facet:
ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp, color = continent)) +
geom_point() +
geom_smooth() +
facet_grid(. ~ continent)## `geom_smooth()` using method = 'loess'
gdpPercap?ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
geom_point() +
geom_smooth() +
geom_text(aes(label = country))## `geom_smooth()` using method = 'gam'
devtools::session_info()## Session info -------------------------------------------------------------
## setting value
## version R version 3.4.3 (2017-11-30)
## system x86_64, darwin15.6.0
## ui X11
## language (EN)
## collate en_US.UTF-8
## tz America/Chicago
## date 2018-03-08
## Packages -----------------------------------------------------------------
## package * version date source
## assertthat 0.2.0 2017-04-11 CRAN (R 3.4.0)
## backports 1.1.2 2017-12-13 CRAN (R 3.4.3)
## base * 3.4.3 2017-12-07 local
## bindr 0.1 2016-11-13 CRAN (R 3.4.0)
## bindrcpp 0.2 2017-06-17 CRAN (R 3.4.0)
## broom * 0.4.3 2017-11-20 CRAN (R 3.4.1)
## cellranger 1.1.0 2016-07-27 CRAN (R 3.4.0)
## cli 1.0.0 2017-11-05 CRAN (R 3.4.2)
## colorspace 1.3-2 2016-12-14 CRAN (R 3.4.0)
## compiler 3.4.3 2017-12-07 local
## crayon 1.3.4 2017-10-03 Github (gaborcsardi/crayon@b5221ab)
## datasets * 3.4.3 2017-12-07 local
## devtools 1.13.5 2018-02-18 CRAN (R 3.4.3)
## digest 0.6.15 2018-01-28 CRAN (R 3.4.3)
## dplyr * 0.7.4.9000 2017-10-03 Github (tidyverse/dplyr@1a0730a)
## evaluate 0.10.1 2017-06-24 CRAN (R 3.4.1)
## forcats * 0.3.0 2018-02-19 CRAN (R 3.4.3)
## foreign 0.8-69 2017-06-22 CRAN (R 3.4.3)
## ggplot2 * 2.2.1 2016-12-30 CRAN (R 3.4.0)
## glue 1.2.0 2017-10-29 CRAN (R 3.4.2)
## graphics * 3.4.3 2017-12-07 local
## grDevices * 3.4.3 2017-12-07 local
## grid 3.4.3 2017-12-07 local
## gtable 0.2.0 2016-02-26 CRAN (R 3.4.0)
## haven 1.1.1 2018-01-18 CRAN (R 3.4.3)
## hms 0.4.1 2018-01-24 CRAN (R 3.4.3)
## htmltools 0.3.6 2017-04-28 CRAN (R 3.4.0)
## httr 1.3.1 2017-08-20 CRAN (R 3.4.1)
## jsonlite 1.5 2017-06-01 CRAN (R 3.4.0)
## knitr * 1.20 2018-02-20 CRAN (R 3.4.3)
## lattice 0.20-35 2017-03-25 CRAN (R 3.4.3)
## lazyeval 0.2.1 2017-10-29 CRAN (R 3.4.2)
## lubridate 1.7.2 2018-02-06 CRAN (R 3.4.3)
## magrittr 1.5 2014-11-22 CRAN (R 3.4.0)
## memoise 1.1.0 2017-04-21 CRAN (R 3.4.0)
## methods * 3.4.3 2017-12-07 local
## mnormt 1.5-5 2016-10-15 CRAN (R 3.4.0)
## modelr 0.1.1 2017-08-10 local
## munsell 0.4.3 2016-02-13 CRAN (R 3.4.0)
## nlme 3.1-131.1 2018-02-16 CRAN (R 3.4.3)
## parallel 3.4.3 2017-12-07 local
## pillar 1.1.0 2018-01-14 CRAN (R 3.4.3)
## pkgconfig 2.0.1 2017-03-21 CRAN (R 3.4.0)
## plyr 1.8.4 2016-06-08 CRAN (R 3.4.0)
## psych 1.7.8 2017-09-09 CRAN (R 3.4.1)
## purrr * 0.2.4 2017-10-18 CRAN (R 3.4.2)
## R6 2.2.2 2017-06-17 CRAN (R 3.4.0)
## Rcpp 0.12.15 2018-01-20 CRAN (R 3.4.3)
## readr * 1.1.1 2017-05-16 CRAN (R 3.4.0)
## readxl 1.0.0 2017-04-18 CRAN (R 3.4.0)
## reshape2 1.4.3 2017-12-11 CRAN (R 3.4.3)
## rlang 0.2.0 2018-02-20 cran (@0.2.0)
## rmarkdown 1.8 2017-11-17 CRAN (R 3.4.2)
## rprojroot 1.3-2 2018-01-03 CRAN (R 3.4.3)
## rstudioapi 0.7 2017-09-07 CRAN (R 3.4.1)
## rvest 0.3.2 2016-06-17 CRAN (R 3.4.0)
## scales 0.5.0 2017-08-24 cran (@0.5.0)
## stats * 3.4.3 2017-12-07 local
## stringi 1.1.6 2017-11-17 CRAN (R 3.4.2)
## stringr * 1.3.0 2018-02-19 CRAN (R 3.4.3)
## tibble * 1.4.2 2018-01-22 CRAN (R 3.4.3)
## tidyr * 0.8.0 2018-01-29 CRAN (R 3.4.3)
## tidyverse * 1.2.1 2017-11-14 CRAN (R 3.4.2)
## tools 3.4.3 2017-12-07 local
## utils * 3.4.3 2017-12-07 local
## withr 2.1.1 2017-12-19 CRAN (R 3.4.3)
## xml2 1.2.0 2018-01-24 CRAN (R 3.4.3)
## yaml 2.1.16 2017-12-12 CRAN (R 3.4.3)
Wickham, Hadley. (2010) “A Layered Grammar of Graphics”. Journal of Computational and Graphical Statistics, 19(1).↩
Wilkinson, Leland. (2005). The Grammar of Graphics. (UChicago authentication required)↩
This exercise is drawn from Wickham, Hadley. (2010) “A Layered Grammar of Graphics”. Journal of Computational and Graphical Statistics, 19(1).↩